package dS.ProjectGPS.LeicaOWI;

import javax.microedition.lcdui.*;

public class FormOwiPRT extends Form implements FormOwi {

  OWIAgent owiAgent = null;

  ChoiceGroup port = new ChoiceGroup("Port", ChoiceGroup.POPUP);
  StringItem note2 = new StringItem("", "Press update after changing port",
                                   StringItem.PLAIN);
  TextField baud = new TextField("Baud Rate", "", 6, TextField.NUMERIC);
  ChoiceGroup parity = new ChoiceGroup("Parity", ChoiceGroup.EXCLUSIVE);
  TextField databit = new TextField("Data Bit", "", 1, TextField.NUMERIC);
  TextField stopbit = new TextField("Stop Bit", "", 1, TextField.NUMERIC);
  ChoiceGroup flowcontrol = new ChoiceGroup("Flow Control",
                                            ChoiceGroup.EXCLUSIVE);
  StringItem note = new StringItem("",
     "Note: Below valid only if Flow Control = RTS Active High",
                                   StringItem.PLAIN);
  TextField leadTime = new TextField("Lead Time", "", 1, TextField.NUMERIC);
  TextField tailTime = new TextField("Tail Time", "", 1, TextField.NUMERIC);

  public FormOwiPRT(OWIAgent o) {
    super("Port Configuration");

    owiAgent = o;

    try {
      ndsInit();
    }
    catch (Exception ex) {
    }
  }

  public void ndsInit() throws Exception {
    port.append("Current Port", null);
    port.append("Port 1", null);
    port.append("Port 2", null);
    port.append("Port 3", null);
    port.append("Terminal Port", null);
    append(port);
    append(note2);
    append(baud);
    append(parity);
    append(databit);
    parity.append("None", null);
    parity.append("Odd", null);
    parity.append("Even", null);
    append(stopbit);
    flowcontrol.append("None", null);
    flowcontrol.append("Xon / Xoff", null);
    flowcontrol.append("CTS / RTS Normal", null);
    flowcontrol.append("RTS High (Always)", null);
    flowcontrol.append("RTS Active High (Waiting for CTS)", null);
    flowcontrol.append("RTS Active High (With delays)", null);
    append(flowcontrol);
    append(note);
    append(leadTime);
    append(tailTime);
  }

  public void update() {
    owiAgent.query(OWI.qsPRT("" + port.getSelectedIndex()));

    port.setSelectedIndex(OWI.prtPort, true);
    baud.setString(OWI.prtBaudRate);
    parity.setSelectedIndex(OWI.prtParity, true);
    databit.setString(OWI.prtDataBits);
    stopbit.setString(OWI.prtStopBits);
    flowcontrol.setSelectedIndex(OWI.prtFlowControl, true);
    leadTime.setString(OWI.prtLeadTimeDelay);
    tailTime.setString(OWI.prtTailTimeDelay);

  }

  public void save() {

    String msgId = "PRT";
    String[] outputs;

    // Setup SET parameters
    if (flowcontrol.getSelectedIndex() == 5) {
      String[] temp = {
                      msgId,
                      "" + port.getSelectedIndex(),
                      baud.getString(),
                      "" + parity.getSelectedIndex(),
                      databit.getString(),
                      stopbit.getString(),
                      "" + flowcontrol.getSelectedIndex(),
                      leadTime.getString(),
                      tailTime.getString()
      };
      outputs = temp;


    }
    else {
      String[] temp = {
                      msgId,
                      "" + port.getSelectedIndex(),
                      baud.getString(),
                      "" + parity.getSelectedIndex(),
                      databit.getString(),
                      stopbit.getString(),
                      "" + flowcontrol.getSelectedIndex(),
      };
      outputs = temp;
    }
    //Send message and update form if successful
    int ackCode = (owiAgent.set(msgId, OWI.setMessage(outputs)));
    Controller.showACKAlert(this, this, ackCode);
  }

}
